﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class panel_DeliveryTerm : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        
        public panel_DeliveryTerm()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);

            LoadData();

            this.DeliveryTerm.DoubleClick += DeliveryTerm_DoubleClick;
            this.DeliveryTerm.PopupMenuShowing += DeliveryTerm_PopupMenuShowing;
            this.deliveryTermContext.ItemClicked += deliveryTermContext_ItemClicked;
        }

        private void callEditForm()
        {
            DataRow row = DeliveryTerm.GetFocusedDataRow();
            frm_Edit_DeliveryTerm editForm = new frm_Edit_DeliveryTerm();
            editForm.delivery_id = row[0].ToString();
            if (editForm.LoadData())
            {
                //editForm.Show();
                editForm.ShowDialog();
                LoadData();
            }
            else
            {
                MessageBox.Show("Cannot load data for selected record");
            }
        }

        void DeliveryTerm_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        void deliveryTermContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.deliveryTermContext.Hide();
            DataRow row = DeliveryTerm.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_DeliveryTerm add_Form = new frm_Add_DeliveryTerm();
                    //add_Form.Show();
                    add_Form.ShowDialog();
                    LoadData();
                    break;
                case "deleteSelection":
                    deleteRecord();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void DeliveryTerm_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.deliveryTermContext.Show(MousePosition);
        }

        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT * FROM [222_DeliveryTerm]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridDeliveryTerm.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                MessageBox.Show("There is no data for Delivery Term");
                DataTable newTable = new DataTable();
                newTable.Columns.Add("DeliveryTermID", typeof(int));
                newTable.Columns.Add("DeliveryTermName", typeof(string));
                newTable.Columns.Add("ExOrIm", typeof(bool));
                newTable.Columns.Add("HandOverPlace", typeof(string));
                gridDeliveryTerm.DataSource = newTable;
            }
            DeliveryTerm.Columns[0].Caption = "Delivery Term ID";
            DeliveryTerm.Columns[1].Caption = "Delivery Term Name";
            DeliveryTerm.Columns[2].Caption = "Export";
            DeliveryTerm.Columns[3].Caption = "Hand Over Place";
            DeliveryTerm.BestFitColumns();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_DeliveryTerm add_Form = new frm_Add_DeliveryTerm();
            //add_Form.Show();
            add_Form.ShowDialog();
            LoadData();
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            deleteRecord();
        }

        private void deleteRecord()
        {
            int[] rows = DeliveryTerm.GetSelectedRows();
            if (rows.Length <= 0)
            {
                MessageBox.Show("There is no selected record to delete");
            }
            else
            {
                if (MessageBox.Show("Are you really to delete selected records now?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string[] id_list = new string[rows.Length];
                    for (int i = 0; i < rows.Length; i++)
                    {
                        DataRow selected_row = DeliveryTerm.GetDataRow(rows[i]);
                        id_list[i] = "'" + selected_row[0].ToString() + "'";
                    }
                    string id = string.Join(",", id_list);

                    try
                    {
                        myConnection.Open();
                        string sql = "DELETE [222_DeliveryTerm] WHERE DeliveryTermID IN (" + id + ")";
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                        cmd_sql.ExecuteNonQuery();
                        cmd_sql.Dispose();
                        myConnection.Close();
                        MessageBox.Show("DeliveryTerm deleted success.");
                        LoadData();
                    }
                    catch //(Exception ex)
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        //MessageBox.Show(ex.Message);
                        MessageBox.Show("Cannot delete DeliveryTerm. Please try again.");
                    }
                }
            }
        }
    }
}
